package com.alstudio.utils.formater;

import java.io.File;

import android.text.TextUtils;

public class FileIdUtils {

	public static String getBareFileId(String url) {
		if (TextUtils.isEmpty(url))
			return null;

		if (!url.contains("http://")) {
			return url;
		}

		String s = "";
		try {
			s = url.substring(url.indexOf("fileid=") + 7);
		} catch (Exception e) {

		}

		return s;
	}

	public static String getLocalFileByFileId(String dir, String fileid) {
		if (TextUtils.isEmpty(fileid)) {
			return null;
		}

		if (fileid.contains("/mnt/sdcard")) {
			return fileid;
		}

		fileid = FileIdUtils.getBareFileId(fileid);

		String fliepath;
		if (dir.endsWith("/")) {
			fliepath = dir + fileid /* + ".jpg" */;
		} else {
			fliepath = dir + "/" + fileid /* + ".jpg" */;
		}

		File f = new File(fliepath);

		String s = null;
		// 如果不存在,那么直接返回空对象
		if (!f.exists() || f.length() == 0) {
			f.deleteOnExit();

		} else {
			s = f.getAbsolutePath();
		}

		return s;
	}
}
